<?php

namespace app\api\controller\yqfff;

use app\admin\model\FffAcc;

/**
 * 一起飞飞飞用户相关接口
 */
class Fffaccs extends BaseFff
{

    // 登录接口
    public function login()
    {
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');

        // 获取openid
        $data = $this->jscode2session($gh_id, $code);
        if ($data) {
            $openid = $data['openid'];
            $session_key = $data['session_key'];
            // token
            $token = md5(md5($openid) . $session_key);

            $acc = FffAcc::get(['openid' => $openid]);
            if (!$acc) {
                $acc = new FffAcc;
                $acc->login_day_num = 1;
                $acc->plane_skin_list = '1';
                $acc->bullet_skin_list = '1';
                $acc->plane_skin = 1;
                $acc->bullet_skin = 1;
            }
            $acc->openid = $openid;
            $acc->session = $session_key;
            $acc->token = $token;

            if ($acc->save() !== false) {
                $return['token'] = $token;
                $this->result('login', $return);
            }
        }
        $this->result('fail');
    }

    // 获取用户信息
    public function get_user_info()
    {
        /**
         * 每天第一次登录判断
         * 登录天数加一
         * 清空签到标志
         * 重置抽奖相关
         */
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y')); // 今天0点
        if ($this->acc->last_login_time < $beginToday) {
            $this->acc->login_day_num += 1;
            $this->acc->is_sign_in = 0;
            $this->acc->lucky_wheel_num = 5;
            $this->acc->lucky_wheel_video = 0;
        }
        $this->acc->last_login_time = time();
        $this->acc->save();

        // 组合返回数据
        $data['openid'] = $this->acc->openid; // openid
        $data['nick_name'] = $this->acc->nick_name ?: ''; // 昵称
        $data['avatar_url'] = $this->acc->avatar_url ?: ''; // 头像
        $data['channel'] = $this->acc->channel ?: ''; // 渠道来源
        $data['guidance'] = $this->acc->guidance; // 新手教程
        $data['max_score'] = $this->acc->max_score; // 最高分
        $data['life_coin'] = $this->acc->life_coin; // 复活币
        $data['bullet_skin'] = $this->acc->bullet_skin; // 当前子弹皮肤id
        $data['plane_skin'] = $this->acc->plane_skin; // 当前飞机皮肤id
        $data['login_day_num'] = $this->acc->login_day_num; // 登录天数
        $data['coin'] = $this->acc->coin; // 金币
        $data['diamond'] = $this->acc->diamond; // 钻石
        // 合并其它数据
        $data = array_merge($data, $this->get_add_resource_num());
        // 签到相关数据
        $data['sign_in_round'] = floor($this->acc->sign_in_num / 7) == 0 ? 1 : 2; // 签到轮数
        $data['is_sign_in'] = $this->acc->is_sign_in; // 今日是否签过到
        if ($this->acc->is_sign_in == 1) {
            $data['sign_in_num'] = floor($this->acc->sign_in_num % 7); // 已签到的天数
        } else {
            $data['sign_in_num'] = floor($this->acc->sign_in_num % 7) + 1; // 下一次签到天数
        }
        // 审核控制
        $data['flag'] = $this->version == $this->control_version ? true : false;
        // 首页展示跳转小程序
        $data['important_list'] = $this->important_list;
        $this->result('data', $data);
    }

    // 获取跳转小程序
    public function get_navigate_list()
    {
        $list = array(
            array(
                'name' => '蹦蹦Jump',
                'appid' => 'wxfc2e85ad6e4c1691',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/bbjump.jpg'
            ),
            array(
                'name' => '阅享每日一文',
                'appid' => 'wx9a21a5ca27f8cbc5',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mryw.jpg'
            ),
            array(
                'name' => '拯救查尔斯',
                'appid' => 'wxc76853fdcf188c3e',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/charles.jpg'
            ),
            array(
                'name' => '天天去钓鱼',
                'appid' => 'wxbdd0d9a5187bc731',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/fish.jpg'
            ),
            array(
                'name' => '重名查询',
                'appid' => 'wx3093c5ec75efcd7b',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/cmcx2.jpg'
            ),
            array(
                'name' => '嗨爆小片',
                'appid' => 'wx38e4080448c98296',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/hbxp.jpg'
            ),
            array(
                'name' => '随你搞',
                'appid' => 'wx07cac980fde4202a',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/sng.jpg'
            ),
            array(
                'name' => '魔盘',
                'appid' => 'wx1ce4c0d710fcec54',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mp.jpg'
            ),
            array(
                'name' => '蹦蹦Jump',
                'appid' => 'wxfc2e85ad6e4c1691',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/bbjump.jpg'
            ),
        );
        $this->result('navigate', $list);
    }

    // 更新玩家头像昵称
    public function update_user_info()
    {
        $nick_name = $this->request->post('nick_name');
        $avatar = $this->request->post('avatar');
        if ($nick_name) {
            $this->acc->nick_name = $nick_name;
        }
        if ($avatar) {
            $this->acc->avatar_url = $avatar;
        }
        $this->acc->save();
        $this->result('ok');
    }

    // 更新新手教程进度
    public function update_guidance()
    {
        $guidance = $this->request->post('guidance') ?: 0;
        $this->acc->guidance = $guidance;
        $this->result('guidance', $this->acc->save());
    }

    // 获取分享信息
    public function get_share_info()
    {
        // 存储分享次数
        $this->acc->share_num += 1;
        $this->acc->save();

        $data = array(
            array(
                'title' => '生活不止眼前的苟且,还有诗和远方的砖块',
                'imgUrl' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/yqfff/share/1.jpg'
            ),
            array(
                'title' => '完全停不下来,快来一起飞飞飞',
                'imgUrl' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/yqfff/share/2.png'
            ),
            array(
                'title' => '快来和我一起飞飞飞吧!',
                'imgUrl' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/yqfff/share/3.png'
            )
        );
        $this->result('ok', $data[array_rand($data, 1)]);
    }

    // 获取世界排行榜数据
    public function get_rank()
    {
        $page = $this->request->post('page');

        $list = FffAcc::whereNotNull('avatar_url')
            ->limit(($page - 1) * 20, 20)
            ->order('max_score', 'desc')
            ->select();
        $rank_list = array();
        foreach ($list as $item) {
            $res['nick_name'] = $item->nick_name;
            $res['avatar_url'] = $item->avatar_url;
            $res['max_score'] = $item->max_score;
            array_push($rank_list, $res);
        }
        $this->result('ok', $rank_list);
    }

    // 获取自己在世界排行的数据
    public function get_self_rank()
    {
        $list = FffAcc::whereNotNull('avatar_url')
            ->limit(0, 100)
            ->order('max_score', 'desc')
            ->select();
        $data['rank_id'] = 0;
        foreach ($list as $key => $item) {
            if ($item->openid == $this->acc->openid) {
                $data['rank_id'] = $key + 1;
            }
        }
        $data['nick_name'] = $this->acc->nick_name;
        $data['avatar_url'] = $this->acc->avatar_url;
        $data['max_score'] = $this->acc->max_score;
        $this->result('ok', $data);
    }

    // 记录渠道进入
    public function handle_channel()
    {
        $channel = $this->request->post('channel');
        if (!$this->acc->channel) {
            $this->acc->channel = $channel;
            $this->result($this->acc->save());
        }
    }

}
